// pages/odetail/odetail.js
import {getOrderInfo, getMyAddress} from '../../apis/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: 0,
    cardId: 0,
    order: {},
    allGoodsNum: 0,
    address: {},
    cardType: '',
    single:'',
    orderTime:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    console.log(options)
    this.setData({
      orderId: parseInt(options.oid),
      cardId: parseInt(options.cid),
      cardType: options.type,
    })
    const res = await getOrderInfo({id: this.data.orderId})

  if(res.data[0].orders.shippingDate!=null){
    res.data[0].orders.shippingDate = res.data[0].orders.shippingDate.substring(0,10)
  }

    this.setData({
      order: res.data[0]
    })
    const request = await getMyAddress({id: this.data.order.orders.addressId, userId: this.data.order.orders.userId})
   
    this.setData({
      address: request.data[0]
    })

    this.getAllGoodsNum()
  },
  cgThisOrder() {
    const oid = this.data.orderId;
    const cid = this.data.cardId;
    const type = this.data.cardType
    wx.navigateTo({
      url: `/pages/changebuy/changebuy?oid=${oid}&cid=${cid}&type=${type}`,
    })
  },
  // 获取所有要下单的商品
  getAllGoodsNum: function () {
    let sum = 0
    for (let i = 0; i < this.data.order.ordersDetailed.length; i++) {
      sum += this.data.order.ordersDetailed[i].quantity
    }
    this.setData({
      allGoodsNum: sum
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})